模組化的架構
重複使用能將軟體從脆弱的線性流程轉化為穩健的可交換組件系統。透過將邏輯抽象成獨立的函數,我們建立了一個 唯一真實來源。這對像 火星探測車的環境監測站(REMS)這類系統尤為關鍵,它必須在不重複撰寫程式碼的情況下處理多面向的資料流。
為什麼函數很重要
將程式碼組織成函數,能讓它更容易理解、重複使用與維護。這遵循了 DRY(不要重複自己) 原則:將感應器原始電壓轉換為攝氏度的邏輯只需定義一次並在各處呼叫,以避免「複製貼上」所造成的錯誤。
快速測驗 12.2
將程式碼拆分成函數有哪些好處?模組化讓開發者可以一次專注於一個邏輯單元,簡化除錯與測試過程。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What are some advantages of splitting code into functions?
It makes the code execute faster by the Go compiler.
It makes the code easier to understand, reuse, and maintain.
It eliminates the need for variable declarations.
It allows functions to run without being called.
✅ Correct!
Correct! Modularity reduces cognitive load and centralizes logic.❌ Incorrect
The primary benefits are architectural: readability, reusability, and maintenance.QUESTION 2
Do you call a function with arguments or parameters?
Parameters
Arguments
Identifiers
Receivers
✅ Correct!
Correct! You pass 'arguments' (the actual values) to a function that has defined 'parameters' (the placeholders).❌ Incorrect
Think of it this way: Parameters are the definitions in the declaration; Arguments are the values used during the call.QUESTION 3
If there existed a groundSensor function that returned a celsius temperature, could it be assigned to a variable of a function type requiring the same signature?
Yes, if the return types and parameters match.
No, because function names must be unique.
Only if the function is exported (Uppercase).
No, functions cannot be assigned to variables in Go.
✅ Correct!
Exactly. In Go, functions are first-class citizens and can be assigned to variables if their signatures match.❌ Incorrect
Go allows functions to be treated as values, provided the signature compatibility is maintained.QUESTION 4
What does the DRY principle stand for?
Data Retrieval Yield
Don't Repeat Yourself
Define Regular Yields
Distributed Runtime Yield
✅ Correct!
DRY ensures that a specific logic is defined once, preventing logic drift and bugs.❌ Incorrect
DRY is about avoiding duplication: Don't Repeat Yourself.QUESTION 5
How does modularity help in managing complex systems like the Mars Rover?
It allows every sensor to use the same physical memory space.
It allows independent logic for different data streams like wind and pressure.
It prevents the rover from needing an operating system.
It makes the hardware lighter.
✅ Correct!
Yes! Each sensor module can be handled by a specific function, keeping the system organized.❌ Incorrect
Modularity is a software design pattern to manage complexity, not a hardware weight reduction method.Case Study: The REMS Data Pipeline
Applying Modularity to Mars Exploration
The Mars Rover REMS needs to gather atmospheric pressure and ground temperature. Currently, the code is a single 500-line main function. You are tasked with refactoring this into modular components.
Q
1. Why would creating a separate 'convertVoltageToCelsius' function be better than doing the math directly inside the main loop?
Solution:
It centralizes the conversion logic. If the sensor calibration changes, you only update one function instead of searching through the entire main loop for every instance of the math.
It centralizes the conversion logic. If the sensor calibration changes, you only update one function instead of searching through the entire main loop for every instance of the math.
Q
2. If we add a third sensor for UV radiation, how does our modular approach simplify this addition?
Solution:
We simply define a new function for the UV sensor that follows the established data format and call it within our existing data collector logic, rather than rewriting the collection framework.
We simply define a new function for the UV sensor that follows the established data format and call it within our existing data collector logic, rather than rewriting the collection framework.